<?php

namespace App\Model;

use App\BaseModel;
use App\User;

class UserPoint extends BaseModel
{
    protected $table = "t_user_points";

    public static function createOneRecord($userId,$point,$desc,$objType=null,$objId=0,$options=[])
    {
        $typeArr = ['withdraw-auto'=>'积分自动提现','withdraw'=>'积分提现','bonus'=>'订单分红','consume'=>'积分消费','dealer'=>'经销商分红'];

        $m = new UserPoint();
        $m->user_id = $userId;
        $m->point = $point;
        $m->desc = $desc;
        $m->obj_type = $objType;
        $m->obj_id = $objId;
        $m->ok = 1;
        $m->objTypeName = isset($typeArr[$m->obj_type])?$typeArr[$m->obj_type]:"";

        if (isset($options['state'])){
            $m->state = $options['state'];
        }
        if (isset($options['stateName'])){
            $m->stateName = $options['stateName'];
        }

        if (isset($options['ok'])){
            $m->ok = $options['ok'];
        }
        $m->save();

        //更新用户积分
        User::updateUserPoint($m->user_id);

        return $m;
    }

}
